package com.jy.adscm.hospital.service.impl;

import com.baomidou.mybatisplus.mapper.Wrapper;
import com.jy.adscm.bean.system.JyCode;
import com.jy.adscm.bean.system.JyUser;
import com.jy.adscm.dealer.bean.Dealer;
import com.jy.adscm.dealer.mapper.DealerMapper;
import com.jy.adscm.hospital.bean.JyHospitalCompeteHospital;
import com.jy.adscm.hospital.bean.JyHospitalRemarks;
import com.jy.adscm.hospital.mapper.JyHospitalCompeteHospitalMapper;
import com.jy.adscm.hospital.service.JyHospitalCompeteHospitalService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jy.adscm.system.mapper.mysql.JyCodeMapper;
import com.jy.adscm.system.mapper.mysql.JyUserMapper;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 竞争对手信息 服务实现类
 * </p>
 *
 * @author yyy
 * @since 2019-01-16
 */
@Service
public class JyHospitalCompeteHospitalServiceImpl extends ServiceImpl<JyHospitalCompeteHospitalMapper, JyHospitalCompeteHospital> implements JyHospitalCompeteHospitalService {
    private Logger logger = Logger.getLogger(getClass());

    @Autowired
    JyUserMapper jyUserMapper;

    @Autowired
    DealerMapper dealerMapper;

    @Autowired
    JyCodeMapper jyCodeMapper;

    @Override
    public List<JyHospitalCompeteHospital> selectList(Wrapper<JyHospitalCompeteHospital> wrapper) {

        logger.info("查询医院竞争对手信息...");

        List<JyHospitalCompeteHospital> jyHospitalCompeteHospitals = super.selectList(wrapper);

        if (jyHospitalCompeteHospitals != null && jyHospitalCompeteHospitals.size()>0){
            for (JyHospitalCompeteHospital jyHospitalCompeteHospital : jyHospitalCompeteHospitals) {
                Integer inputUserId = jyHospitalCompeteHospital.getInputUserId();
                if (inputUserId != null){
                    JyUser user = jyUserMapper.selectById(inputUserId);
                    if (user != null){
                        jyHospitalCompeteHospital.setInputUserName(user.getName());
                    }
                }

                Integer inputUnit = jyHospitalCompeteHospital.getInputUnit();
                if (inputUnit == 0){
                    jyHospitalCompeteHospital.setInputUnitDesc("生产厂商");
                }else {
                    Dealer dealer = dealerMapper.selectById(inputUnit);
                    if (dealer != null){
                        jyHospitalCompeteHospital.setInputUnitDesc(dealer.getName());
                    }
                }

                Long hospitalType = jyHospitalCompeteHospital.getHospitalType();
                if (hospitalType != null){
                    List<JyCode> hospital_type = jyCodeMapper.getJyCodeByCodeType("hospital_type");
                    if (hospital_type != null && hospital_type.size()>0){
                        for (JyCode jyCode : hospital_type) {
                            long code = Long.parseLong(jyCode.getCode());

                            if (code ==hospitalType){
                                jyHospitalCompeteHospital.setHospitalTypeDesc(jyCode.getName());
                            }
                        }
                    }
//                    JyCode jyCode = jyCodeMapper.selectById(hospitalType);
//                    if (jyCode != null){
//                        jyHospitalCompeteHospital.setHospitalTypeDesc(jyCode.getName());
//                    }

                }

                Long hospitalLevel = jyHospitalCompeteHospital.getHospitalLevel();
                if (hospitalType != null){
                    List<JyCode> hospital_type = jyCodeMapper.getJyCodeByCodeType("hospital_level");
                    if (hospital_type != null && hospital_type.size()>0){
                        for (JyCode jyCode : hospital_type) {
                            long code = Long.parseLong(jyCode.getCode());

                            if (code == hospitalLevel){
                                jyHospitalCompeteHospital.setHospitalLevelDesc(jyCode.getName());
                            }
                        }
                    }

//                    JyCode jyCode = jyCodeMapper.selectById(hospitalLevel);
//                    if (jyCode != null){
//                        jyHospitalCompeteHospital.setHospitalLevelDesc(jyCode.getName());
//                    }

                }
            }
        }

        return jyHospitalCompeteHospitals;
    }
}
